/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.declex;


import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

import com.activeharmony.query.Select;
import com.chinasoft_ohos.commontools.toast.Toast;
import com.dspot.declex.annotation.Event;
import com.dspot.declex.annotation.Model;
import com.dspot.declex.annotation.Populate;
import com.dspot.declex.event.UserEventBean_;
import com.example.declex.model.RestBean_;
import com.example.declex.model.User_;

import org.ohosannotations.annotations.AfterInject;
import org.ohosannotations.annotations.AfterViews;
import org.ohosannotations.annotations.Click;
import org.ohosannotations.annotations.ComponentById;
import org.ohosannotations.annotations.EAbility;

import java.lang.reflect.Method;
import java.util.List;

import timber.log.Timber;

import static com.example.declex.Action.$PopulateViewsAbility;
import static com.example.declex.Action.$ServerModelAbility;

@EAbility(ResourceTable.Layout_ability_demo)
public class DemoAbility extends Ability {
    /*  查询列表@Model(query = "Id > 0")
   List<User_> users;*/

    @Model(query = "Id = 1")
    User_ user;//注入被@UseModel的model

    @ComponentById(ResourceTable.Id_tv_name)
    Text tvName;

    @ComponentById(ResourceTable.Id_tv_age)
    Text tvAge;

    @ComponentById(ResourceTable.Id_tv_email)
    Text tvEmail;

    @ComponentById(ResourceTable.Id_tv_description)
    TextField tvDescription;
    int AGE = 22;
    //@Click
    //$ServerModelAbility server_model; //回报这个异常scanner.scan(treePath, trees);Exception: begin 0, end -4, length 0

    @AfterInject
    void afterInject() {//user被注入之后回调
        user.setName("Alice");
        user.setEmail("alice@example.com");
        user.setAge(AGE);
    }

    @AfterViews
    void afterView() {
        if (user != null) {
            tvName.setText(String.format("name：%s", user.getName()));
            tvAge.setText(String.format("age：%s", user.getAge()));
            tvEmail.setText(String.format("email：%s", user.getEmail()));
            tvDescription.setText(user.getDescription());
        }
    }

    @Click
    void save() {
        user.setName(tvName.getText());
        user.setAge(AGE);
        user.setEmail(tvEmail.getText());
        user.setDescription(tvDescription.getText());
        user.save();
        //样式丑$Toast("save succeed");
        Toast.show("save succeed");
    }


    @Click
    void event() {
        //这样也行UserEventBean_.post();
        UserEventBean_.create().setName(user.getName()).postEvent();
    }

    @Click
    void server_model() {
        $ServerModelAbility();
    }

    @Event
    void onUserEventBean(String name) {//接收事件,生成userEventBean事件类,成员变量name
        //样式丑$Toast("name=" + name);
        Toast.show("name=" + name);
    }

    @Click
    void populate_view() {
        $PopulateViewsAbility().withResult(100);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            User_ user = new Select().from(User_.class).where("Id=?", 1).executeSingle();
            if (user != null) {
                tvDescription.setText(user.getDescription());

            }
        }
    }
}
